(function(){
    angular.module("bookListCtrlModule",[])
        .controller("bookListController",["$scope","IMAGEURL",
            "getDataService","userDataService","$location",
            function($scope,IMAGEURL,getDataService,
                     userDataService,$location){
                $scope.IMAGEURL = IMAGEURL;
                $scope.userservice = userDataService.userData;
                getDataService.getRequest(
                    "book.php",
                    null,
                    function(response){
                        console.log(response);
                        if(response.code==0){
                            $scope.books = response.data;
                        }else {
                            console.log(data.data);
                        }
                    },
                    function(error){
                        console.log(error);
                    }
                );

                $scope.loginOut = function(){
                    console.log("loginOut");
                    userDataService.userData = {};
                    $scope.userservice = {};
                    $location.path("/bookList");
                    
                }
                 $scope.cartList = {
     
                  };
                  $scope.cartNum = 0;
                  
                  
                  
                  $scope.total = 0;


                  $scope.buy = function(good){
                        if($scope.cartList[good.id]){
                              $scope.cartList[good.id]['num']++; 
                        }else{
                              $scope.cartList[good.id] = {"gid":good.id,"name":good.title,"price":good.price,"num":1}
                        }
                  }
                  $scope.cartJian = function(gid){
                $scope.cartList[gid]['num']--;
                if($scope.cartList[gid]['num']<=0){
                      delete $scope.cartList[gid];
                }
          }

            $scope.cartJia = function(gid){

                $scope.cartList[gid]['num']++;
            }
                          $scope.$watch("cartList",function(){
                        var total = 0;
                        var num = 0;
                        for(var i in $scope.cartList){
           
                              num++;
                              total += $scope.cartList[i]['price']*$scope.cartList[i]['num'];
                        }


                        $scope.total = total;
                     
                        $scope.cartNum = num;
                  },true)
            }
        ])
})();